<?php

class Control_ScrollerController extends Zend_Controller_Action {

    public function init() {
        $this->_helper->layout->setLayout('admin');
        $this->yesButton = 'Yes'; //напис на кнопці Так під час видалення
        $this->noButton = 'No'; //напис на кнопці Ні під час видалення
        $this->dbTable = new Control_Model_Table_Scroller();
        $this->view->controller = $this->getRequest()->getControllerName();
        $this->view->action = $this->getRequest()->getActionName();
    }

    public function indexAction() {
        $this->_helper->redirector('edit');
    }

    public function addAction() {
        $form = new Control_Model_Form_Scroll();
        $form->submit->setLabel('Add');
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {

                $name = $form->getValue('name');
                $text = $form->getValue('text');

                $adapter = $form->getElement('image')->getTransferAdapter();
                $file = $adapter->getFileInfo();
                $ext = split("[/\\.]", $file['image']['name']);
                $valueForReplace = array(" ", ".");
                $newName = str_replace($valueForReplace, "", microtime()) . '.' . $ext[1];
                $adapter->addFilter('Rename', realpath(dirname('.')) .
                        DIRECTORY_SEPARATOR .
                        'images' .
                        DIRECTORY_SEPARATOR .
                        'user' .
                        DIRECTORY_SEPARATOR .
                        'original' .
                        DIRECTORY_SEPARATOR .
                        $newName);
                $adapter->receive($file['image']['name']);
                $image = '/' . $file['image']['destination'] . '/' . $newName;

                $link = $form->getValue('link');
                $this->scrollDb->addScroll($name, $text, $image, $link);
                $this->_helper->redirector('edit');
            } else {
                $form->populate($formData);
            }
        }
    }

    public function editAction() {
        $this->view->PortfolioData = $this->dbTable->getScroller();
        $form = new Control_Model_Form_Scroll();
        $form->submit->setLabel('Update');
        $this->view->form = $form;
        $delparam = $this->_getParam('del', 0);

        if ($this->getRequest()->isPost() && ($delparam != 1)) { //Якщо відправлена форма без видалення
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $id = (int) $form->getValue('id');
                $name = $form->getValue('name');
                $text = $form->getValue('text');
                $adapter = $form->getElement('image')->getTransferAdapter();
                $file = $adapter->getFileInfo();
                if ($file['logo']['name'] != "") {
                    $ext = split("[/\\.]", $file['logo']['name']);
                    $valueForReplace = array(" ", ".");
                    $newName = str_replace($valueForReplace, "", microtime()) . '.' . $ext[1];
                    $adapter->addFilter('Rename', realpath(dirname('.')) .
                            DIRECTORY_SEPARATOR .
                            'images' .
                            DIRECTORY_SEPARATOR .
                            'user' .
                            DIRECTORY_SEPARATOR .
                            'original' .
                            DIRECTORY_SEPARATOR .
                            $newName);
                    $adapter->receive($file['logo']['name']);
                    $image = '/' . $file['logo']['destination'] . '/' . $newName;
                } else {
                    $image = "";
                };
                $link = $form->getValue('link');
                $this->dbTable->updateScroll($id, $name, $text, $image, $link);
                $this->_helper->redirector('edit');
            } else {
                $form->populate($formData);
            }
        } elseif ($this->getRequest()->isPost()) { //Якщо відправлена форма з видаленням
            if ($this->getRequest()->isPost()) {
                $del = $this->getRequest()->getPost('del');
                if ($del == $this->yesButton) {
                    $id = $this->getRequest()->getPost('id');
                    $this->dbTable->deleteData($id);
                } else {
                    $this->_helper->redirector('edit');
                }
                $this->_helper->redirector('edit');
            }
        } else {//Виведення сторінки едіт
            $id = (int) $this->_getParam('id', 0);
            $del = (int) $this->_getParam('del', 0);
            $this->view->get_id = $id;
            if ($id > 0 && $del == 0) {
                $formdata = $this->dbTable->getDataID($id);
                if (isset($formdata)) {
                    $form->populate($formdata);
                } else {
                    $this->view->get_id = 0;
                };
            } elseif ($id > 0 && $del == 1) {
                $this->view->get_del = 1;
                $this->view->yesButton = $this->yesButton;
                $this->view->noButton = $this->noButton;
                $this->view->NameToDelete = $this->dbTable->getDataID($id);
            } elseif ($id == 0) {
                $this->view->get_id = $id;
            } else {
                $this->view->get_id = 0;
                echo "<span style='color: red;'>Error id: $id<br></span>";
            }
        }
    }

}

